package com.crayon2f.test;

import com.crayon2f.test.beans.City;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by feifan.gou@gmail.com on 2022/3/23 14:45.
 */
public class TEST2022_03 {

    @Test
    public void test() {

        Map<String, List<City>> childrenMap = ImmutableMap.of(
                "北京", Lists.newArrayList(
                        new City("北京-1"), new City("北京-2"),
                        new City("北京-3"), new City("北京-4")
                ),
                "上海", Lists.newArrayList(
                        new City("上海-1"), new City("上海-2"),
                        new City("上海-3"), new City("上海-4")
                ),
                "沧州", Lists.newArrayList(
                        new City("沧州-1"), new City("沧州-2"),
                        new City("沧州-3"), new City("沧州-4")
                ),
                "常州", Lists.newArrayList(
                        new City("常州-1"), new City("常州-2"),
                        new City("常州-3"), new City("常州-4")
                )
                ,
                "张家口", Lists.newArrayList(
                        new City("张家口-1"), new City("张家口-2"),
                        new City("张家口-3"), new City("张家口-4")
                )
        );
        List<City> list = Lists.newArrayList(
                new City("北京"), new City("上海"), new City("常州")
        );
        List<City> list2 = Lists.newArrayList(
                new City("沧州"), new City("张家口")
        );

        Stream<City> concat = Stream.concat(list.stream().parallel(), list2.stream().parallel());
        List<City> all = concat.flatMap(city -> {
            String name = city.getName();
            System.out.println("父原" + name);
            List<City> cities = childrenMap.get(name);
            city.setName("合计_" + cities.size());
            System.out.println("父现" + city.getName());
            cities.add(city);
            return cities.stream();
        }).collect(Collectors.toList());
        System.out.println(all);
    }

    @Test
    public void flatMap() {

        Stream<String> stream = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "7").stream();
        Stream<String> stream2 = Lists.newArrayList("a", "b", "c", "d", "e", "f", "g").stream();

        System.out.println(Stream.concat(stream, stream2)
                .flatMap(ths -> {
                    System.out.println(ths);
                    return Lists.newArrayList(ths + "add").stream();
                }).collect(Collectors.toList()));
    }
}
